<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Product extends Model
{
    const STATUS=1 ; // 是否推荐：1推荐，0否'

    //获取热门
    static public  function getHotProducts($limit,$user)
    {

      return  self::where('status',self::STATUS)
          ->with(['collect'=>function($query) use($user){
              $query->where('users_id',$user);
          }])
        ->paginate($limit,false);
    }

    public function collect()
    {
        return $this->belongsTo(Collect::class,'id','products_id');
    }

    public function comments()
    {
        return $this->hasMany(Comments::class,'product_id','id');
    }

    public function cate()
    {
        return $this->hasOne(Category::class,'id','category_id');
    }

    public function getBannersAttr($value)
    {
        return empty($value) ? $value : explode(',',$value);
    }
}
